package example;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Set;
import java.util.Stack;

@Mojo(name = "checkSnapshot", defaultPhase = LifecyclePhase.PACKAGE)
public class CheckSnapshotMojo extends AbstractMojo {

    @Component
    private MavenProject project;

    XMLInputFactory factory = XMLInputFactory.newInstance();

    public void execute() throws MojoExecutionException {

        MavenProject projectClone = project.clone();
        ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_COMPILE);
        projectClone.setArtifactFilter(artifactFilter);
        // 获取项目的全部依赖
        Set<Artifact> artifacts = projectClone.getArtifacts();
        for (Artifact artifact : artifacts) {
            File jar = artifact.getFile();
            File pom = new File(jar.getParentFile(), jar.getName().replace(".jar", ".pom"));

            if (pom.exists()) {
                String parent = getParent(pom);
                if (parent != null && parent.contains(Artifact.SNAPSHOT_VERSION)) {
                    getLog().warn("find a hoodlum , parent is SNAPSHOT: " + artifact.getGroupId() + ":" + artifact.getArtifactId());
                }
            }
        }
    }
    public String getParent(File pom) {
        XMLStreamReader reader = null;
        try {
            InputStream xmlInputStream = new FileInputStream(pom);
            reader = factory.createXMLStreamReader(xmlInputStream);
            // 用栈跟踪位置
            Stack<String> stack = new Stack<>();
            while (reader.hasNext()) {
                int event = reader.next();
                if (event == XMLStreamConstants.START_ELEMENT) {
                    String localName = reader.getLocalName();
                    stack.add(localName);
                } else if (event == XMLStreamConstants.END_ELEMENT) {
                    stack.pop();
                } else if (event == XMLStreamConstants.CHARACTERS) {
                    if ("[project, parent, version]".equals(stack.toString())) { // 偷个懒，简易的判断方法
                        return reader.getText();
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
